package top.heliming.rabbitmq.rabbitmqdemo.topic;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import top.heliming.rabbitmq.rabbitmqdemo.util.ConnectionUtil;

/**
 * description: //订阅模式：生产者指定路由key SEND RECV2
 *
 * @author: heliming
 * @date:2019/06/07 上午 10:24
 */
public class Send {

  private final static String EXCHANGE_NAME = "topic_exchange_test";

  public static void main(String[] argv) throws Exception {
    send();
  }

  public static void send() throws Exception{
    // 获取到连接
    Connection connection = ConnectionUtil.getConnection();
    // 获取通道
    Channel channel = connection.createChannel();
    // 声明exchange，指定类型为topic
    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    // 消息内容
    String message = "新增商品 : id = 1001";
    // 发送消息，并且指定routing key 为：insert ,代表新增商品
    channel.basicPublish(EXCHANGE_NAME, "item.insert", null, message.getBytes());
    System.out.println(" [商品服务：] Sent '" + message + "'");
    String messageupdate = "修改商品 : id = 1001";
    // 发送消息，并且指定routing key 为：insert ,代表新增商品
    channel.basicPublish(EXCHANGE_NAME, "item.update", null, messageupdate.getBytes());
    System.out.println(" [商品服务：] Sent '" + messageupdate + "'");
    String messagedelete = "删除商品 : id = 1001";
    // 发送消息，并且指定routing key 为：insert ,代表新增商品
    channel.basicPublish(EXCHANGE_NAME, "item.delete", null, messagedelete.getBytes());
    System.out.println(" [商品服务：] Sent '" + messagedelete + "'");
    channel.close();
    connection.close();
  }
}
